package com.dtx.msg;

import java.util.Objects;

public class ResultMsg<T> {
    private static Integer SUCCESS;
    private static Integer ERROR;
    public static final String MSG = "操作成功";
    private Integer code;
    private String msg;
    private T data;

    public ResultMsg() {
    }

    public ResultMsg(ResultMsgEnum em) {
        this.code = em.getCode();
        this.msg = em.getMessage();
    }

    public ResultMsg(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultMsg(T data) {
        this.code = SUCCESS;
        this.msg = "操作成功";
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ResultMsg<?> codeMsg = (ResultMsg<?>) o;
        return Objects.equals(code, codeMsg.code) && Objects.equals(msg, codeMsg.msg) && Objects.equals(data, codeMsg.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, msg, data);
    }



    public static ResultMsg success() {
        return new ResultMsg(SUCCESS, "操作成功");
    }

    public static ResultMsg success(String msg) {
        return new ResultMsg(SUCCESS, msg);
    }

    public static ResultMsg fail(String msg) {
        return new ResultMsg(ERROR, msg);
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "CodeMsg{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    static {
        SUCCESS = ResultMsgEnum._200.getCode();
        ERROR = ResultMsgEnum._500.getCode();
    }
}
